from rest_framework.views import APIView
from rest_framework.response import Response
from blog.models import Visitors


class GetDataView(APIView):
    def get(self, request, *args, **kwargs):
        try:
            query = Visitors.objects.filter(status=1).order_by("-id")[:7]
            vis_list = []
            for vis in query:
                res = {'date': vis.create_time, 'number': vis.visitors}
                vis_list.append(res)
            vis_list.reverse()
            return Response({
                'status': 200,
                'msg': '获取成功',
                'data': vis_list
            })
        except Exception as e:
            return Response({'status': 204, 'msg': '遇到了异常错误', 'err': e.args})
